package com.android.stockme.frontend;

import com.android.stockme.frontend.R;

import android.app.Activity;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TabHost;

import com.android.stockme.backend.BackendController;
import com.android.stockme.backend.IBackendController;

/**
 * 
 * @author Avishay
 *
 * This class is the application's entry point. It initializes all the other tabs which the application's user interface is assembled of. 
 *
 */
public class StockMeTabActivity extends TabActivity {
	
	/**
	 * @uml.property  name="handlerDecorator"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="stockMeTabActivity:com.android.stockme.frontend.HandlerDecorator"
	 */
	private HandlerDecorator handlerDecorator = null;

	/** 
	 * @uml.property name="backendController"
	 * @uml.associationEnd multiplicity="(1 1)" inverse="stockMeTabActivity:com.android.stockme.backend.IBackendController"
	 */
	private IBackendController backendController = null;
	private static Activity __sharedActivity = null;
	
	private static final String[] ACTIVITY_NAME_ARRAY = { "StockMePortfolioTab", "StockMeNewsTab", "StockMeSettingsTab" };
	private static final String[] ACTIVITY_TAB_LABEL = { "Stocks Portfolio", "News Updates", "Settings" };
	private static final String FRONTEND_PACKAGE_NAME = "com.android.stockme.frontend";
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        __sharedActivity = this;
        
        this.handlerDecorator = new HandlerDecorator(new Handler());
        this.backendController = new BackendController.BackendControllerBuilder(this.handlerDecorator).build();
        
        
        this.setContentView(R.layout.main);
        
        this.setDefaultTab(0);
        
        TabHost host = this.getTabHost();
        
        for (int i = 0; i < ACTIVITY_NAME_ARRAY.length; i++) {
        	TabHost.TabSpec tab = host.newTabSpec(ACTIVITY_NAME_ARRAY[i]);

        	ComponentName activity = new ComponentName(FRONTEND_PACKAGE_NAME, FRONTEND_PACKAGE_NAME + "." + ACTIVITY_NAME_ARRAY[i]);

        	tab.setContent(new Intent().setComponent(activity));
        	tab.setIndicator(ACTIVITY_TAB_LABEL[i]);

        	host.addTab(tab);
        	
        }
        
        this.backendController.startUpdating();
    }

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	static public Activity activity(){
		return __sharedActivity;
	}

}